<?php
class Zendvn_Model_Table_Slider extends Zend_Db_Table{
	protected $_name = "slider";
	protected $_primary = "id"; 
	protected $_arrParam = array();

	public function itemInSelect($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()->from($this->_name,array('id','name'));
			if($arrParam['action'] == 'edit'){
				$select->where('id != ' . $arrParam['id']);	
			}
			$result = $db->fetchAll($select);
			$data = new Zendvn_Recursive($result);		
			$result = $data->process();
			if($options == null){
				array_unshift($result, array('id'=>0,'name'=>' Root ','level'=>1));
			}
		}

		if($options['task'] == 'for-product'){
			$db = $this->getAdapter();
			$select = $db->select()->from($this->_name,array('id','name'));
			$result = $db->fetchPairs($select);
		}
		return $result;
	}

	public function sortItem($arrParam = null,$options = null){
		if($options == null){
			foreach ($arrParam['cid'] as $val){				
				$where = 'id = ' . $val;
				$data = array('ordering'=>$arrParam['ordering'][$val]);
				$this->update($data, $where);
			}
		}
	}

	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';													
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);
		}

		if($options['task'] == 'multi'){
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);
					}
				}
			}
		}
	}

	public function deleteItem($arrParam = null,$options = null){
		if($options == null){
			if((int)$arrParam['id']>0){
				$itemInfo = $this->getItem(array('id'=>$arrParam['id']));
				$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 	  		'fileConfig'=>'slider.ini'));
				$file = new Zendvn_File_Upload();
				$avatarConfig = $config->getConfig('slider');
				$file->remove($avatarConfig['uploadDir'],$itemInfo['picture']);				
				$file->remove($avatarConfig['thumbDir'],$itemInfo['picture']);	

				$where = 'id = ' . (int)$arrParam['id'];
				$this->delete($where);
			}
		}

		if($options['task'] == 'multi'){		
			if(count($arrParam['cid'])>0){	
				$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
														 	  		'fileConfig'=>'slider.ini'));						
				$file = new Zendvn_File_Upload();
				foreach ($arrParam['cid'] as $val){
					$itemInfo = $this->getItem(array('id'=>$val));					
					$avatarConfig = $config->getConfig('slider');
					$file->remove($avatarConfig['uploadDir'],$itemInfo['picture']);				
					$file->remove($avatarConfig['thumbDir'],$itemInfo['picture']);	
					$where = 'id = ' . (int)$val;
					$this->delete($where);
				}
			}
		}
	}

	public function getItem($arrParam = null,$options = null){
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'item'){
			$where = 'id = ' . (int)$arrParam['parents'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from($this->_name . ' AS c1')						
						->where('c1.id = ?',$arrParam['id'],1);
			$result = $db->fetchRow($select);		
		}
		if($options['task'] == 'admin-info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from(array('c' => $this->_name))
						->joinLeft(array('c1' => $this->_name), 'c1.id = c.parents', array('c1.name as parents_name'))
						->where('c.id = ?', $arrParam['id'], INTEGER);
			$result = $db->fetchRow($select);
		}
		if($options['task'] == 'product-list'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from($this->_name,array('id','name','parents'))
						->where('parents = ?', $arrParam['cat_id']);
			$result = $db->fetchAll($select);
			if(count($result) > 0){
				return $result;
			}else{
				$select = $db->select()
						->from($this->_name,array('id','name','description'))
						->where('id = ?', $arrParam['cat_id']);
				$result = $db->fetchRow($select);
				return $result;
			}
		}

		if($options['task'] == 'category-child'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from($this->_name,array('id','name','description'))
						->where('id = ?', $arrParam['cat_id']);
			$result = $db->fetchRow($select);
		}
		return $result;
	}

	public function listItem($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from($this->_name);						 

			$arrFilter = $arrParam['arrFilter'];
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where('status = ?',$arrFilter['status']);
			}
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}
			if($arrParam['action'] == 'edit'){
				$select->where('id != ' . $arrParam['id']);	
			}
			$this->_listSQL = $select;			
			$result = $db->fetchAll($select);
		}
		return $result;
	}
	
	public function listSlides($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()
					 ->from($this->_name)
					 ->where('status = ?', 'active')
					 ->order('ordering ASC');
		$result = $db->fetchAll($select);
		return $result;
	}
	public function listBanner($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()
					 ->from($this->_name)
					 ->where('status = ?', 'active')
					 ->where('cat_id = 1')
					 ->order('id DESC');
		$result = $db->fetchRow($select);
		return $result;
	}
	
		public function listAdv($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()
					 ->from($this->_name)
					 ->where('status = ?', 'active')
					 ->where('cat_id = 2')
					 ->order('id DESC');
		$result = $db->fetchRow($select);
		return $result;
	}
	public function listCategory($arrParam = null,$options = null){
		if($options['task'] == 'nav'){
			$db = $this->getAdapter();
			$select = $db->select()->from($this->_name,array('id','name','parents'))
			->where('status = ?', 'active')->where('cities_id=?',$arrParam['cityInfo']['id']);
			$result = $db->fetchAll($select);	
		}
		if($options['task'] == 'category-child'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from($this->_name,array('id','name','picture'))
						->where('parents = ?', $arrParam['cat_id']);
			$result = $db->fetchAll($select);
		}
		return $result;
	}

	public function saveItem($arrParam = null,$options = null){
		$function  = Zendvn_System_Function::call($options['task']);
		$this->$function($arrParam);
	}

	public function adminAdd($arrParam = null){
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 'fileConfig'=>'slider.ini',
													 'section'=>'db'));	
		$language = new Zendvn_Languages();
		$lang = $language->getLangDefault();
		$str = new Zendvn_Filter_Filter();
		$row = $this->fetchNew();
		$row->name			= $arrParam['name'];
		$row->link			= $arrParam['link'];
		$row->description	= $arrParam['description'];
		$row->picture		= $arrParam['picture'];					
		$row->ordering		= ($arrParam['ordering']=='') ? $config->getConfig('ordering'):$arrParam['ordering'];
		$row->status		= ($arrParam['status']=='') ? $config->getConfig('status'):$arrParam['status'];
		$row->save();
	}

	public function adminEdit($arrParam = null){
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 'fileConfig'=>'slider.ini',
													 'section'=>'db'));	
		$language = new Zendvn_Languages();
		$lang = $language->getLangDefault();
		$str = new Zendvn_Filter_Filter();
		$where = 'id = ' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);	
		$row->name			= $arrParam['name'];
		$row->link			= $arrParam['link'];
		$row->description	= $arrParam['description'];
		$row->picture		= $arrParam['picture'];					
		$row->ordering		= ($arrParam['ordering']=='') ? $config->getConfig('ordering'):$arrParam['ordering'];
		$row->status		= ($arrParam['status']=='') ? $config->getConfig('status'):$arrParam['status'];
		$row->save();
	}

}